var color = 'blue';

function changeColor() {
    if (color === 'blue') {
        color = 'red';
    } else {
        color = 'blue';
    }
}
changeColor();
console.log(color) // red ? color是全局的

if (true) {
    var inside = 'I am inside';
}
console.log('outside', inside); // outside I am inside

for (var i = 0; i < 10; i++) {

}
console.log(i); // 10

withoutVar = 'withoutVar';
console.log(withoutVar) // withoutVar
function add(num1, num2) {
    sum = num1 + num2;
    return sum;
}
var result = add(1, 2);
console.log(result);

var a = 1

function geta() {
    var a = 2;
    a = window.a
    return a;
}
console.log(geta())
console.log(a)

var color = ['blue', 'green', 'red'];
color.length = 2; // 删除一个元素
console.log(color);
color[color.length] = 'red'; //增加一个元素
console.log(color);


function compare(obj1, obj2) {
    if (obj1.value < obj2.value) {
        return -1;
    } else if (obj1.value > obj2.value) {
        return 1;
    } else {
        return 0;
    }
}
var obj1 = new Object();
var obj2 = new Object();
var obj3 = new Object();
obj1.value = 1;
obj2.value = 3;
obj3.value = 2;
objArr = [obj1, obj2, obj3];
objArr.sort(compare);
console.log(objArr)

// 数值类型
function compareNum(num1, num2) {
    return num2 - num1; // 降序
    // return num1 - num2; 升序
}
arr = [2, 3, 1, 4, 5, 2, 5];
arr.sort(compareNum)
console.log(arr);
console.log(arr.slice(1, 2));

var nums = [1, 2, 3, 4, 5, 4, 3, 2, 1];
// 要找的项是4 -> 得到的是索引3 
console.log(nums.indexOf(4));

var now = new Date();
console.log(now)
var b = Date.parse('November 1,2019');
console.log(b)

var re = null,
    i;
for (i = 0; i < 10; i++) {
    re = /cat/g;
    var result = re.test('catastrophe');
    console.log(result);
}

var test = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;
var matches = pattern.exec(test);
console.log(matches.index); // 匹配项所在位置
console.log(matches.input); // 应用正则表达式的字符串
console.log(matches);
console.log(matches[0]);
console.log(matches[1]);
console.log(matches[2]);